﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XJay.Identity.Model.Page;

namespace XJIE.Schedule.Common.Helper.Page
{
    public static class Page
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public static async Task<PageResult<T>> ToPageResultAsync<T>(this ISugarQueryable<T> sqlSugarClient,PageParam pageParam)
        {
            PageResult<T> pageResult = new ();
            RefAsync<int> total = 0;
            pageResult.PageNumber = pageParam.PageNumber;
            pageResult.PageSize = pageParam.PageSize;
            pageResult.Result =  await sqlSugarClient.ToOffsetPageAsync(pageParam.PageNumber, pageParam.PageSize, total);
            pageResult.TotalPage = (int)Math.Ceiling((decimal)total / pageParam.PageSize);
            pageResult.TotalCount = total;
            return pageResult;
        }
    }
}
